import re


from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from finallpro.items import FinallproItem,amazonItem
from price import price as pr
class AmazonSpider(CrawlSpider):
    name = 'amazon'
    allowed_domains = ['amazon.cn']
    start_urls = ['http://www.amazon.cn/','http://www.amazon.cn/gp/site-directory/ref=topnav_sad','http://www.amazon.cn/gp/product/B007KI5W26']

    rules = (
        Rule(SgmlLinkExtractor(allow=(r'/dp/',r'/product/'),deny=(r'/dp/images')),callback='amazon',follow=True),
		Rule(SgmlLinkExtractor(allow=(r'node=\d+',r'/b/',r'/s/'))),
    )

    def amazon(self,response):
        hxs=HtmlXPathSelector(response)
        i=amazonItem()
        i['type']='amazon'
        title=hxs.select("//span[@id='btAsinTitle']/text()")
        if title:
            i['title']=title.extract()[0]
        else:
            i['title']='fuckdsjh'
        price=hxs.select("//span[@id='actualPriceValue']/b[@class='priceLarge']/text()")
        if price:
            price=price.extract()[0]
            i['price']=pr(price)
        else:
            i['price']=00000000000000000000
        img=hxs.select("//img[@id='prodImage']/@src")
        if img:
            i['img']=img.extract()[0]
        else:
            i['img']=00000000000000000000000
        id=hxs.select("//input[@id='ASIN']/@value")
        if id:
            i['tid']=id.extract()[0]
        else:
            i['tid']=00000000000000000
            li=hxs.select("//div[@class='bucket']/div[@class='content']/ul/li")
            tmp=li[1]
            if tmp:
                i["lei"]=tmp.extract()
            else:
                i["lei"]='error'
        return i
